/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package narutodownloader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author mamat-dev
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        if (args.length < 2) {
            System.out.println("Example: java -jar OneMangaDownloader.jar Naruto 1 2 200");
        } else {
            String manga = args[0];
            for (int i = 1; i < args.length; i++) {
                String chapter = args[i];
                System.out.println("Prepare Download " + manga + " Chapter " + args[i]);
                String url = "http://www.onemanga.com/" + manga + "/" + chapter + "/";
                HTMLParser html = new HTMLParser(url);
                System.out.print("Check " + manga + " Chapter " + args[i] + " is ....");
                String context = html.getHtmlParser();
                //cek ada yang baru tidak
                Pattern pattern = Pattern.compile("is not available yet");
                Matcher m = pattern.matcher(context);
                if (m.find()) {
                    System.out.println("..... [Not Available]");

                } else {
                    System.out.println("..... [Available]");
                    System.out.println("Download " + manga + " Chapter " + args[i] + "");
                    Runtime rt = Runtime.getRuntime();
                    Process proc;
                    try {
                        String cmd = "mkdir " + manga + "";
                        System.out.println(cmd);
                        proc = rt.exec(cmd);
                        Thread.sleep(1000);

                    } catch (Exception ex) {
                        System.out.println("Error:" + ex);
                    }

                    try {
                        String cmd = "mkdir " + manga + "/" + args[i] + "";
                        System.out.println(cmd);
                        proc = rt.exec(cmd);
                        Thread.sleep(1000);

                    } catch (Exception ex) {
                        System.out.println("Error:" + ex);
                    }

                    int j = 1;
                    boolean check = true;
                    while (check) {

                        String page = "";

                        if (j <= 9) {
                            page = "0" + j;
                        } else {
                            page = "" + j;
                        }

                        String imgurl = url + page + "/";
                        HTMLParser imghtml = new HTMLParser(imgurl);
                        String wgetimgurl = imghtml.getImageParser();

                        //System.out.println("url:"+imgurl+";wget:"+wgetimgurl);

                        try {
                            if (wgetimgurl.length() > 10) {
                                String cmd = "wget " + wgetimgurl + " -O  ./" + manga + "/" + args[i] + "/" + page + ".jpg";
                                System.out.println(cmd);
                                proc = rt.exec(cmd);
                                //System.out.println("Wait for " + proc.waitFor());
                                Thread.sleep(Long.parseLong("" + proc.waitFor()));
                            } else {
                                if (j <= 9) {
                                    page = "0" + j + "-" + "0" + (j + 1);
                                } else {
                                    page = "" + j + "-" + (j + 1);
                                }

                                String imgurlx = url + page + "/";
                                HTMLParser imghtmlx = new HTMLParser(imgurlx);
                                String wgetimgurlx = imghtmlx.getImageParser();

                                if (wgetimgurlx.length() > 10) {
                                    String cmd = "wget " + wgetimgurlx + " -O  ./" + manga + "/" + args[i] + "/" + page + ".jpg";
                                    System.out.println(cmd);
                                    proc = rt.exec(cmd);
                                    //System.out.println("Wait for " + proc.waitFor());
                                    Thread.sleep(Long.parseLong("" + proc.waitFor()));
                                    j++;
                                } else {
                                    System.out.println("wget:" + wgetimgurl);
                                    check = false;
                                }

                            }
                        } catch (Exception ex) {
                            System.out.println("Error:" + ex);
                        }
                        j++;
                    }
                }
            //System.out.println(html.getImageParser());
            }
        }
    }
}
